//
//  ZYStatusRetweetView.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/14.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit
import SnapKit


// 转发微博视图
class ZYStatusRetweetView: UIView {
    
    // 记录底部约束
    var retweetViewBottom: Constraint?
    
    
    var statusViewModel:ZYStatusViewModel? {
        didSet{
            
//            contentLable.text = statusViewModel?.retWeetContent
            
            // 设置富文本
            contentLable.attributedText = statusViewModel?.retweetAttributedString
            
//            pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
              retweetViewBottom?.uninstall()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0{
          
                // 显示配图
                pictureView.hidden = false
                // 设置图片信息
                pictureView.picUrls = picUrls
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
            } else {
 
                pictureView.hidden = true
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(contentLable).offset(StatusTableViewCellMargin).constraint
                })
                
            }
            
        }
    }
    
   // 转发微博内容
    private lazy var contentLable: UILabel = {
        let lable = UILabel(textColor: UIColor.grayColor(), font: HomeFontSize)
        lable.numberOfLines = 0
        lable.text = "hahahahaah"
        return lable
    
    }()
    
    // 配图视图
    private lazy var pictureView: ZYStatusPictureView = {
        let view = ZYStatusPictureView()
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
            setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUpUI() {
    self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(contentLable)
        addSubview(pictureView)
        
        // 设置约束
        contentLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLable)
            make.top.equalTo(contentLable.snp_bottom).offset(StatusTableViewCellMargin)
   
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        // 设置自身约束
        self.snp_makeConstraints { (make) -> Void in
           self .retweetViewBottom =  make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
        
    }
    
}

